﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace kb_24_the_game
{
    internal class Level2
    {
        #region Variabelen Declaratie
        private Biertje biertje;
        private ContentManager content;
        private XML_IO xml = new XML_IO();
        private Backgrounds scrollBackground1;
        private Player speler;
        private IList<ISprite> allSprites;
        private List<Enemies> enemiesList = new List<Enemies>();
        private Enemies jongen1, meisje1;
        private SpriteFont font;
        private Rectangle spelerRect;
        private KeyboardState state;
        private List<ObstakelTest> obstakellijst;
        private bool bovencollision = false, up = false;
        private Rectangle oldCollisionRectangle;
        #endregion Variabelen Declaratie

        public Level2(ContentManager content)
        {
            this.content = content;
            allSprites = new List<ISprite>();
            obstakellijst = new List<ObstakelTest>(); 
            speler = new Player(content, new Vector2(50, 425));
            obstakellijst = xml.readXML(@"XML/level2.xml");

            foreach (ObstakelTest obstakel in obstakellijst)
            {
                obstakel.content = content;
            }
          
            meisje1 = new Enemies(content, 1, new Vector2(1500, 449));
            jongen1 = new Enemies(content, 2, new Vector2(1000, 447));
            biertje = new Biertje(content, new Vector2(150, 400), 2);
            font = content.Load<SpriteFont>("AlgemeneTextures/tussenSchermTekst");
            
            Load();
        }

        private void Load()
        {
            scrollBackground1 = new Backgrounds(content.Load<Texture2D>("Achtergronden/BackgroundLevel02"), new Rectangle(0, 0, 3500, 600));
            allSprites.Add(scrollBackground1);
            //
            foreach (ObstakelTest obstakel in obstakellijst)
            {
                allSprites.Add(obstakel);
            }
            allSprites.Add(jongen1);
            allSprites.Add(meisje1);
            allSprites.Add(biertje);
            allSprites.Add(speler);
            //
            enemiesList.Add(jongen1);
            enemiesList.Add(meisje1);
        }

        public bool Update(GameTime gameTime)
        {
            spelerRect = new Rectangle((int)speler.playerPosition.X, (int)speler.playerPosition.Y, speler.texture.Width / 4, speler.texture.Height / 2);
            CheckKeys();
            speler.Springen(up);

            CheckCollisionsMetBiertjes(spelerRect);
            biertje.CheckPositionsSpelerBiertje(speler.playerPosition);
            CheckCollisionsSpelerMetEnemies(spelerRect);
            if (obstakellijst[0].colliderRect != null)
            {
                CheckCollisionsSpelerMetBank(spelerRect);
                speler.BovenCollision(bovencollision);
            }
            speler.Update(gameTime);
            foreach (Enemies e in enemiesList)
            {
                e.SetSpelerDirectie(speler.loopDirectie);
                e.Update(gameTime);
            }
            foreach (ObstakelTest obstakel in obstakellijst)
            {
                obstakel.Update(gameTime);
            }
            
            return CheckEindCondities();
        }

        private void CheckKeys()
        {
            state = Keyboard.GetState();
            if (state.IsKeyDown(Keys.Z))
            {
                biertje.aantalBiertjes = 5;
            }
            if (state.IsKeyDown(Keys.Up))
            {
                up = true;
            }
            else
            {
                up = false;
            }
            if (state.IsKeyDown(Keys.Left))
            {
                if (scrollBackground1.rectangle.X <= -1)
                {
                    scrollBackground1.Links();
                }
                if(speler.playerPosition.X >=50)
                {
                    speler.Links();
                    biertje.BeweegLinks();
                    foreach (ObstakelTest bank in obstakellijst)
                    {
                        bank.BeweegLinks();
                    }
                }
            }
            if (state.IsKeyDown(Keys.Right))
            {
                if (scrollBackground1.rectangle.X >= -2699)
                {
                    scrollBackground1.Rechts();
                }
                if (speler.playerPosition.X <= 500)
                {
                    speler.Rechts();
                    biertje.BeweegRechts();
                    foreach (ObstakelTest bank in obstakellijst)
                    {
                        bank.BeweegRechts();
                    }
                }
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            foreach (ISprite sprite in allSprites)
            {
                sprite.Draw(spriteBatch);
            }
            if (CheckCollisionsMetDeur(spelerRect) && biertje.aantalBiertjes >= 5)
            {
                spriteBatch.DrawString(font, "Druk op Spatie om naar binnen te gaan.", new Vector2(scrollBackground1.rectangle.X + scrollBackground1.rectangle.Width - 500, 347), Color.White);
            }

        }

        private void CheckCollisionsMetBiertjes(Rectangle spelerRect)
        {
            Rectangle bier = new Rectangle((int)biertje.position.X, (int)biertje.position.Y, biertje.texture.Width, biertje.texture.Height);
            Rectangle overlap = Rectangle.Intersect(spelerRect, bier);
            if (!overlap.IsEmpty)
            {
                biertje.Geraakt(scrollBackground1.rectangle.X, scrollBackground1.rectangle.Width);
            }
        }

        private bool CheckCollisionsMetDeur(Rectangle spelerRect)
        {
            Rectangle deur = new Rectangle(scrollBackground1.rectangle.X + scrollBackground1.rectangle.Width - 367, 387, 100, 100);
            Rectangle overlap = Rectangle.Intersect(spelerRect, deur);
            if (!overlap.IsEmpty && overlap.Width >= spelerRect.Width)
            {
                return true;
            }
            return false;
        }

        private bool CheckEindCondities()
        {
            if (CheckCollisionsMetDeur(spelerRect))
            {
                if (state.IsKeyDown(Keys.Space) && biertje.aantalBiertjes >= 5)
                {
                    return true;
                }
            }
            return false;
        }

        private void CheckCollisionsSpelerMetEnemies(Rectangle spelerRect)
        {
            foreach (Enemies e in enemiesList)
            {
                Rectangle rect1 = new Rectangle((int)e.enemyPosition.X, (int)e.enemyPosition.Y, e.texture.Width / 4, e.texture.Height);
                Rectangle overlap = Rectangle.Intersect(spelerRect, rect1);
                if (!overlap.IsEmpty && oldCollisionRectangle.IsEmpty)
                {
                    biertje.ResetBiertje(scrollBackground1.rectangle.X, scrollBackground1.rectangle.Width);
                }
                oldCollisionRectangle = overlap;
            }
        }
        private void CheckCollisionsSpelerMetBank(Rectangle spelerRect)
        {
            bovencollision = false;
            foreach (ObstakelTest bank in obstakellijst)
            {
                foreach (Rectangle r in bank.colliderRect)
                {
                    Rectangle overlap = Rectangle.Intersect(spelerRect, r);

                    if (!overlap.IsEmpty && overlap.Height < 10 && overlap.Width > 20)
                    {
                        bovencollision = true;
                    }
                }
            }
        }

    }
}
